<template>
  <div class="tizi-city" ref="target">
    <div class="select" @click="toggleDialog()" :class="{active:visible}">
      <span v-if="!fullLocation" class="placeholder">{{placeholder}}</span>
      <span v-else class="value">{{fullLocation}}</span>
      <i class="iconfont icon-angle-down"></i>
    </div>
    <!-- <div class="option" v-if="active">
      <span class="ellipsis" v-for="i in 24" :key="i">北京市</span>
    </div> -->
    <div class="option" v-if="visible">
      <div v-if="loading" class="loading"></div>
      <template v-else>
        <span class="ellipsis" @click="changeItem(item)" v-for="item in currList" :key="item.code">{{item.name}}</span>
      </template>
    </div>
  </div>
</template>

<script>
import {computed, reactive, ref} from 'vue'
import { onClickOutside } from '@vueuse/core'
import axios from 'axios'
export default {
    name:'TiziCity',
    props :{
        fullLocation: {
            type:String,
            default:''
        },
        placeholder: {
            type:String,
            default:'请选择配送地址'
        }
    },
    setup (props, { emit }) {
        // 显示隐藏数据
        const visible = ref(false)

        // 获取城市数据
        // 1. 数据在哪里？https://yjy-oss-files.oss-cn-zhangjiakou.aliyuncs.com/tuxian/area.json
        // 2. 何时获取？打开城市列表的时候，做个内存中缓存
        // 3. 怎么使用数据？定义计算属性，根据点击的省份城市展示
        const getCityData = () => {
            // 这个位置可能有异常操作，封装成promise
            return new Promise((resolve, reject) => {
                if(window.cityData) {
                    // 有缓存
                    resolve(window.cityData)
                }else {
                    // 无缓存
                    const url = 'https://yjy-oss-files.oss-cn-zhangjiakou.aliyuncs.com/tuxian/area.json'
                    axios.get(url).then(res => {
                        window.cityData = res.data
                        resolve(window.cityData)
                    })
                }
            })
        }
        // 显示和隐藏数据
        const loading = ref(false)
        const cityData = ref([])
        const openDialog =() => {
            visible.value = true
            loading.value = true
            // 获取数据
            getCityData().then(data => {
                cityData.value = data
                loading.value = false
            })
            // 清空之前选择
            for (const key in changeResult) {
                changeResult[key] = ''
            }
        }
        const closeDialog = () => {
            visible.value = false
        }
        // 切换展开收起
        const toggleDialog = () => {
            if(visible.value) closeDialog()
            else openDialog()
        }
        // 点击其他位置隐藏
        const target  = ref(null)
        onClickOutside(target, () => {
            closeDialog()
        })


        // 监听用户点击省，市
        const changeResult = reactive({
            provinceCode:'',
            provinceName:'',
            cityCode:'',
            cityName:'',
            countCode:'',
            countName:'',
            fullLocation:''
        })

        const changeItem = (item) => {
            // 省份
            if(item.level === 0 ) {
                changeResult.provinceCode = item.code
                changeResult.provinceName = item.name
            }
            // 市
            if(item.level === 1) {
                changeResult.cityCode = item.code
                changeResult.cityName = item.name
            }
            // 地区
            if (item.level === 2) {
                changeResult.countCode = item.code
                changeResult.countName = item.name
                // 完整路径
                changeResult.fullLocation = `${changeResult.provinceName} ${changeResult.cityName} ${changeResult.countName}`
                // 这是最后一级，选完了，关闭对话框，通知父组件数据
                closeDialog()
                emit('change', changeResult)
            }
        }

         // 定义计算属性
        const currList = computed(() => {
            // 省份
            let currList = cityData.value
            // 城市
            if( changeResult.provinceCode) {
                currList = currList.find(p => p.code === changeResult.provinceCode).areaList
            }
            // 地区
            if (changeResult.cityCode) {
                currList = currList.find(c => c.code === changeResult.cityCode).areaList
            }
            // TODO 根据点击的省份城市获取对应的列表
            return currList
        })

        return {target,visible,toggleDialog,currList,loading,changeItem}
    }
}
</script>

<style lang="less" scoped>
.tizi-city {
  display: inline-block;
  position: relative;
  z-index: 400;  
  .select {
    border: 1px solid #e4e4e4;
    height: 30px;
    padding: 0 5px;
    line-height: 28px;
    cursor: pointer;
    &.active {
      background: #fff;
    }
    .placeholder {
      color: #999;
    }
    .value {
      color: #666;
      font-size: 12px;
    }
    i {
      font-size: 12px;
      margin-left: 5px;
    }
  }
  .option {
    width: 542px;
    border: 1px solid #e4e4e4;
    position: absolute;
    left: 0;
    top: 29px;
    background: #fff;
    min-height: 30px;
    line-height: 30px;
    display: flex;
    flex-wrap: wrap;
    padding: 10px;
    > span {
      width: 130px;
      text-align: center;
      cursor: pointer;
      border-radius: 4px;
      padding: 0 3px;
      &:hover {
        background: #f5f5f5;
      }
    }
    .loading {
        height: 290px;
        width: 100%;
        background: url(../../assets/images/loading1.gif) no-repeat center;
    }
  }
}
</style>